using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace Tetris
{
    public partial class PathForm : Form
    {
        private int sizeX, sizeY, sizeXq;

        public PathForm()
        {
            InitializeComponent();

            Engine.NewPath += new EventHandler(FormPath_Invalidate);
        }

        void FormPath_Invalidate(object sender, EventArgs e)
        {
            Invalidate();
        }

        void RecalculateSize()
        {
            sizeX = this.ClientSize.Width / Engine.mainBoard.Size.Width;
            sizeY = this.ClientSize.Height / Engine.mainBoard.Size.Height;
            sizeXq = sizeX >> 2;
        }

        private void PathForm_Resize(object sender, EventArgs e)
        {
            RecalculateSize();
            Invalidate();
        }

        private void PathForm_Paint(object sender, PaintEventArgs e)
        {
            e.Graphics.Clear(Color.Black);

            for (int y = 0; y < Engine.mainBoard.Size.Height; y++)
            {
                for (int x = 0; x < Engine.mainBoard.Size.Width; x++)
                {
                    for (int i = 0; i < 4; i++)
                    {
                        if (Engine.possibilities[x, y].IsDirection(i))
                            e.Graphics.FillEllipse(
                                    new SolidBrush(Color.Green),
                                    sizeX * x + i * sizeXq, sizeY * (Engine.mainBoard.Size.Height - 1 - y),
                                    sizeXq, sizeY);
                    }

                    e.Graphics.DrawLine(
                        new Pen(Color.Gray),
                        new Point((int)(sizeX * x), 0),
                        new Point((int)(sizeX * x), ClientSize.Height));
                }
            }
        }        

        private void PathForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            this.Visible = false;
            if (e.CloseReason == CloseReason.UserClosing)
                e.Cancel = true;
        }
    }
}